package nl.tomcee.skinprops;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import net.miginfocom.swing.MigLayout;
import se.datadosen.jalbum.Frog;
import se.datadosen.jalbum.JAlbumContext;
import se.datadosen.jalbum.SkinProperties;

/**
 * Reads and writes the skin.properties file
 * @since jalbum 8.7
 * @author Tom
 * @version 1.7
 */
public class SkinPropsEditor extends JFrame {

    final static String APP_NAME = "Skin Properties Editor";
    final static String VERSION = "1.7";
    final static String AUTHOR = "Tom";
    final String SIMPLE_NAME = getClass().getSimpleName();
    /**
     ***************
     * init fields *
     ***************
     */
    private JAlbumContext context = JAlbumContext.getInstance();
    private String skin = context.getSkinDir().getName();
    private String accountProfileName = context.getEngine().getAccountProfileName();
    private JTextField title = new JTextField("", 40);
    private JTextField supportForum = new JTextField("", 40);
    private JTextField author = new JTextField("", 40);
    private JCheckBox autoWidgetInjection = new JCheckBox();
    private JTextField company = new JTextField("", 40);
    private JTextArea description = new JTextArea() {

        {
            setRows(5);
            setColumns(40);
            setLineWrap(true);
            setWrapStyleWord(true);
            setFont(new java.awt.Font("Tahoma", 0, 11));
        }
    };
    private JScrollPane spDescription = new JScrollPane() {

        {
            setViewportView(description);
        }
    };
    private JTextField homePage = new JTextField("", 40);
    private JTextField requiredJalbumVersion = new JTextField("", 40);
    private JTextField requiredJavaVersion = new JTextField("", 40);
    private JTextField userName = new JTextField("", 40);
    private JTextField version = new JTextField("", 40);
    private JPanel panel;
    private SkinProperties skinProperties = context.getEngine().getSkinProperties();
    private Date currentDate = new Date();
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    private DefaultFormatterFactory dff = new DefaultFormatterFactory(new DateFormatter(formatter));
    private String now = formatter.format(currentDate);
    private JFormattedTextField creationDate = new JFormattedTextField(dff);
    private JFormattedTextField lastModifiedDate = new JFormattedTextField(dff);
    private JButton saveButton = new JButton("Save") {

        {
            addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    saveSkinProps();
                    dispose();
                }
            });
        }
    };
    private JButton cancelButton = new JButton("Cancel") {

        {
            addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    dispose();
                }
            });
        }
    };
    private JButton nowButton = new JButton("now!") {

        {
            addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    lastModifiedDate.setText(now);
                }
            });
            addMouseListener(new java.awt.event.MouseAdapter() {

                @Override
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    setText(now);
                }

                @Override
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    setText("now!");
                }
            });
        }
    };

    /**
     ****************
     * init methods *
     ****************
     */
    //
    /**
     *
     * @return menubar
     */
    private JMenuBar buildMenu() {
        JMenuBar menubar = new JMenuBar();
        JMenuItem aboutMenuItem = new JMenuItem("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                new Frog().ribbit();
                String aboutText = "<html><b>" + APP_NAME
                        + "<br>version: " + VERSION
                        + "<br>author: " + AUTHOR
                        + "</b></html>";
                JOptionPane.showMessageDialog(rootPane, aboutText,
                        "About: " + SIMPLE_NAME,
                        JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().
                        getResource("tclogo.gif")));
            }
        });
        menubar.add(aboutMenuItem);
        return menubar;
    }

    /**
     *
     * @return panel
     */
    private JPanel createPanel() {
        panel = new JPanel() {

            {
                setLayout(new MigLayout("wrap 2"));
                add(new JLabel(SkinProperties.TITLE));
                add(title);
                add(new JLabel(SkinProperties.VERSION));
                add(version);
                add(new JLabel(SkinProperties.AUTHOR));
                add(author);
                add(new JLabel(SkinProperties.CREATION_DATE));
                add(creationDate, "split 2");
                add(new JLabel("(YYYY-MM-DD)"));
                add(new JLabel(SkinProperties.LAST_MODIFIED_DATE));
                add(lastModifiedDate, "split 3");
                add(new JLabel("(YYYY-MM-DD)"));
                add(nowButton);
                add(new JLabel(SkinProperties.AUTO_WIDGET_INJECTION));
                add(autoWidgetInjection);
                add(new JLabel(SkinProperties.REQUIRED_JALBUM_VERSION));
                add(requiredJalbumVersion);
                add(new JLabel(SkinProperties.REQUIRED_JAVA_VERSION));
                add(requiredJavaVersion);
                add(new JLabel(SkinProperties.COMPANY));
                add(company);
                add(new JLabel(SkinProperties.USERNAME));
                add(userName);
                add(new JLabel(SkinProperties.HOME_PAGE));
                add(homePage);
                add(new JLabel(SkinProperties.SUPPORT_FORUM));
                add(supportForum);
                add(new JLabel(SkinProperties.DESCRIPTION));
                add(spDescription);
                add(saveButton, "cell 1 13, split 2, align right");
                add(cancelButton);
            }
        };
        return panel;
    }

    private void getSkinProps() {

        title.setText(skinProperties.getProperty(SkinProperties.TITLE));
        author.setText(skinProperties.getProperty(SkinProperties.AUTHOR));
        autoWidgetInjection.setSelected(Boolean.valueOf(skinProperties.getProperty(SkinProperties.AUTO_WIDGET_INJECTION)));
        company.setText(skinProperties.getProperty(SkinProperties.COMPANY));
        creationDate.setText(skinProperties.getProperty(SkinProperties.CREATION_DATE));
        description.setText(skinProperties.getProperty(SkinProperties.DESCRIPTION));
        homePage.setText(skinProperties.getProperty(SkinProperties.HOME_PAGE));
        supportForum.setText(skinProperties.getProperty(SkinProperties.SUPPORT_FORUM));
        lastModifiedDate.setText(skinProperties.getProperty(SkinProperties.LAST_MODIFIED_DATE));
        requiredJalbumVersion.setText(skinProperties.getProperty(SkinProperties.REQUIRED_JALBUM_VERSION));
        requiredJavaVersion.setText(skinProperties.getProperty(SkinProperties.REQUIRED_JAVA_VERSION));
        userName.setText(skinProperties.getProperty(SkinProperties.USERNAME));
        version.setText(skinProperties.getProperty(SkinProperties.VERSION));
        if (title.getText().equals("")) {
            title.setText(skin);
        }
        if (creationDate.getText().equals("")) {
            creationDate.setText(now);
        }
        if (lastModifiedDate.getText().equals("")) {
            lastModifiedDate.setText(now);
        }
        if (userName.getText().equals("") && !accountProfileName.equals("")) {
            userName.setText(accountProfileName.substring(accountProfileName.lastIndexOf("- ") + 2));
        }
        if (requiredJavaVersion.getText().equals("")) {
            requiredJavaVersion.setText("1.5");
        }
    }

    private void saveSkinProps() {
        skinProperties.setProperty(SkinProperties.TITLE, title.getText());
        skinProperties.setProperty(SkinProperties.AUTHOR, author.getText());
        skinProperties.setProperty(SkinProperties.AUTO_WIDGET_INJECTION, String.valueOf(autoWidgetInjection.isSelected()));
        skinProperties.setProperty(SkinProperties.COMPANY, company.getText());
        skinProperties.setProperty(SkinProperties.CREATION_DATE, creationDate.getText());
        skinProperties.setProperty(SkinProperties.DESCRIPTION, description.getText());
        skinProperties.setProperty(SkinProperties.HOME_PAGE, homePage.getText());
        skinProperties.setProperty(SkinProperties.SUPPORT_FORUM, supportForum.getText());
        skinProperties.setProperty(SkinProperties.LAST_MODIFIED_DATE, lastModifiedDate.getText());
        skinProperties.setProperty(SkinProperties.REQUIRED_JALBUM_VERSION, requiredJalbumVersion.getText());
        skinProperties.setProperty(SkinProperties.REQUIRED_JAVA_VERSION, requiredJavaVersion.getText());
        skinProperties.setProperty(SkinProperties.USERNAME, userName.getText());
        skinProperties.setProperty(SkinProperties.VERSION, version.getText());
        try {
            skinProperties.save();
            System.out.println(new Date() + " skin.properties saved succesfull.");
        } catch (IOException ex) {
            System.out.println(ex + " - failed to write skin.properties");
        }
    }

    /**
     *********************
     * application logic *
     *********************
     */
    public SkinPropsEditor() {
        setTitle(skin + " - " + APP_NAME);
        setIconImage(new ImageIcon(getClass().
                getResource("tclogo.gif")).getImage());
        setLocationByPlatform(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        setJMenuBar(buildMenu());
        getSkinProps();
        getContentPane().add(createPanel());

        pack();
        setVisible(true);

        description.setCaretPosition(0);
        cancelButton.requestFocusInWindow();

        //System.out.println(SIMPLE_NAME + " loaded at " + new Date());
    }
}//

